package com.vsd.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.dialet.PaginationSupport;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.vsd.daoEx.model.IndexActStoreExWithStore;
import com.vsd.domain.IndexActStore;
import com.vsd.service.IndexActStoreService;

@Controller
@RequestMapping("indexActStore")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class IndexActStoreController extends CoreAdminController{
	@Autowired
	private IndexActStoreService indexActStoreService;
	
	@RequestMapping("/list")
	@ResponseBody
	public String citylist(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
		PaginationSupport.byPage(start/length+1, length);
		List<IndexActStoreExWithStore> list = indexActStoreService.selectExList(areaCode);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
  
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String string : ids) {
			indexActStoreService.delById(string);
		}
		return JSONUtils.serialize(new Ret(0));
	}


	@RequestMapping("/update")
	@ResponseBody
	public String update(String id,Integer state,String remark,Integer sortOrder){
		IndexActStore g = new IndexActStore();
		g.setId(id);
		g.setState(state);
		g.setRemark(remark);
		g.setSortOrder(sortOrder);
		
		indexActStoreService.updateByPrimaryKey(g);
		
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(String id,HttpSession session){
		IndexActStore s = indexActStoreService.findByStoreIdAndActId(getSessionParameter("id", session), id);
		if(s!=null) return JSONUtils.serialize(new Ret(0));
		IndexActStore store = new IndexActStore();
		store.setActId(id);
		store.setId(StringUtil.getUUID());
		store.setCreateTime(new Date());
		store.setState(1);
		store.setStoreId(getSessionParameter("id", session));
		indexActStoreService.save(store);
		return JSONUtils.serialize(new Ret(0));
	}
}
